package com.lin.java.tool.common.model;

import com.lin.java.tool.common.enums.RespCode;
import lombok.Data;

/**
 * @作者: 林江
 * @创建时间: 2024/12/17
 * @功能: R响应对象
 */
@Data
public class R<T> {

    /**
     * 响应码
     */
    private Integer code;
    /**
     * 响应状态
     */
    private String msg;
    /**
     * 响应数据
     */
    private T data;

    public R(Integer code, String msg, T data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }


    public static <T> R<T> ok() {
        return ok(RespCode.SUCCESS.getCode(), RespCode.SUCCESS.getDesc(), null);
    }

    public static <T> R<T> ok(T data) {
        return ok(RespCode.SUCCESS.getCode(), RespCode.SUCCESS.getDesc(), data);
    }

    public static <T> R<T> ok(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public static <T> R<T> fail() {
        return ok(RespCode.FAIL.getCode(), RespCode.FAIL.getDesc(), null);
    }

    public static <T> R<T> fail(T data) {
        return fail(RespCode.FAIL.getCode(), RespCode.FAIL.getDesc(), data);
    }

    public static <T> R<T> fail(Integer code, String msg, T data) {
        return new R<>(code, msg, data);
    }

    public static <T> R<T> fail(RespCode respCode, T data) {
        return fail(respCode.getCode(), respCode.getDesc(), data);
    }
}